package com.finalproject.ui;
/**
 * Cafe for UI
 */
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import com.finalproject.entity.Cafe;
import com.finalproject.entity.Cafe.CafeException;
import com.finalproject.entity.Cafe.CafeObserver;
import com.finalproject.entity.CafeItem;
import com.finalproject.entity.CaloriesExceededException;
import com.finalproject.entity.CardHolder;
import com.finalproject.entity.InsufficientFundsException;

public class UICafe extends JPanel implements CafeObserver{
	

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;



	private  UICafeItemPanel uiCafeItemPanel;
	
	
	
	private Cafe cafe;
	
	private CardHolder cardHolder = null;
	
	
	
	public UICafe(Cafe cafe) {
		cafe.addCafeObserver(this);
		
		setBorder(BorderFactory.createTitledBorder("Cafe #" + cafe.getId() + " " + cafe.getName()));
		setLayout(new BorderLayout(3,3));
		
		uiCafeItemPanel = new UICafeItemPanel();
		uiCafeItemPanel.setBorder(BorderFactory.createTitledBorder("Menus"));
		this.cafe = cafe;
		
		
		
		add(uiCafeItemPanel,BorderLayout.CENTER);
		setPreferredSize(new Dimension(600,700));
		loadCafeItem(cafe);
		

	}
	
	public void setCardHolder(CardHolder cardHolder) {
		this.cardHolder = cardHolder;
	}
	
	
	
	private void loadCafeItem(final Cafe cafe) {
		uiCafeItemPanel.removeAll();
		for(String name : cafe.getCafeItemNames()) {
			final CafeItem cafeItem=cafe.getCafeItemsByName(name);
			
			UICafeItem uiCafeItem = new UICafeItem(cafeItem);
			uiCafeItem.getBuyButton().addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					buySnackItem(cafe, cafeItem);
					
				}
			});
			uiCafeItemPanel.add(uiCafeItem);
			
			
		}
		
		revalidate();
		repaint();
	}


	
	private void buySnackItem(Cafe cafe,  CafeItem cafeItem) {
		String snackName = cafeItem.getName();
		try {
			if(cardHolder != null) {
				cardHolder.buyItem(cafeItem);
				 cafe.buyCafeItem(snackName);
				
				Calendar now = Calendar.getInstance();
				now.add(Calendar.SECOND, cafeItem.getPrepTime());
				CafeItemScheduler cafeItemScheduler = new CafeItemScheduler(cafeItem) ;
				cafeItemScheduler.schedule();
				UIHelper.showConfirmMessage(this, "Your order " + snackName + " will be ready in " +  cafeItem.getPrepTime() + " minutes \n at " + "Cafe #" + cafe.getId() + " " + cafe.getName() + " location" + "\n"
				);
				
				
			} else {
				showPayCashDialog(cafe, cafeItem);
			}
			
		} catch (CafeException e) {
			
			e.printStackTrace();
		} catch(CaloriesExceededException cee) {
			UIHelper.showErrorMessage(this, "Calories Exceeded!!");
			
		} catch(InsufficientFundsException iee) {
			UIHelper.showErrorMessage(this, "Insufficient Fund!!");
		} catch(Exception e) {
			e.printStackTrace();
		}
		
	}

	

	
	private class CafeItemScheduler{
		private CafeItem cafeItem;
		public CafeItemScheduler(CafeItem cafeItem) {
			this.cafeItem = cafeItem;
		}
		
		public void schedule() {
			final Timer timer = new Timer();
			int prepTime = cafeItem.getPrepTime();
			final Calendar prepTimeCal = Calendar.getInstance();
			//System.out.println("before preTImeCal:" + prepTimeCal.getTime());
			prepTimeCal.set(Calendar.SECOND, prepTimeCal.get(Calendar.SECOND)+prepTime);
			//System.out.println("afeter preTImeCal:" + prepTimeCal.getTime());
			
			timer.schedule(new TimerTask() {
				
				@Override
				public void run() {
					Calendar now = Calendar.getInstance();
					UIHelper.showConfirmMessage(getThis(), "Your Order " + cafeItem.getName() + " is Ready!!");
					
				
					
				}
			},prepTimeCal.getTime());
		}
		
	}
	
	public JPanel getThis() {
		return this;
	}


	

	@Override
	public void onUpdate(Cafe cafe) {
		loadCafeItem(cafe);
		
		
	}
	
	
	
	
	
	
	
	private void showPayCashDialog(final Cafe cafe, CafeItem cafeItem) {
		int response = JOptionPane.showConfirmDialog(this,
				"You are not Card Holder Member. Would you like to pay in Cash?\n" 
				+ "Total is " + UIHelper.currencyFormat.format(cafeItem.getPrice()),
				"Confirm", JOptionPane.YES_NO_OPTION,
				JOptionPane.QUESTION_MESSAGE);
		if (response == JOptionPane.NO_OPTION) {
			
		} else if (response == JOptionPane.YES_OPTION) {
			try {
				Calendar now = Calendar.getInstance();
				cafe.buyCafeItem(cafeItem.getName());
				CafeItemScheduler cafeItemScheduler = new CafeItemScheduler(cafeItem) ;
				cafeItemScheduler.schedule();
				UIHelper.showConfirmMessage(this, "Your order " + cafeItem.getName() + " will be ready in " +  cafeItem.getPrepTime() + " minutes \n at " + "Cafe #" + cafe.getId() + " " + cafe.getName() + " location" + "\n"
						);
						
			} catch (CafeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
	
	

}
